import 'package:flutter/material.dart';
import 'package:flutter_uxin/Login/LoginRoot.dart';

import 'dart:ui';

import 'package:flutter_uxin/Tools/config/commonTool.dart';
import 'package:flutter_uxin/base/baseTab.dart';

import 'package:flutter_uxin/addressBook/addressBook.dart';
import 'package:flutter_uxin/My/settingPage.dart';
import 'package:flutter_uxin/Message/chatScreen.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Future<String> getUserProfile_name() async {
    String user_name = await commonTool.getPreferencesValue('profile_name');

//    print('user_name------------$user_name');
    return user_name;
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: FutureBuilder<String>(
        future: getUserProfile_name(),
        builder: _buildFuture,
      ),

      //注册路由
      routes: {
        "addressBook": (context) => addressBookPage(title: '通讯录'),
        "settingPage": (context) => settingPage(title: '设置'),
        "LoginRoot": (context) => LoginRootPage(title: '登录'),
        "chatScreen": (context) => chatScreenPage(title: '聊天'),
      },
      //错误路由处理，统一返回UnknownPage
//      onUnknownRoute: (RouteSettings setting) => MaterialPageRoute(builder: (context) => UnknownPage()),
    );
  }

  Widget _buildFuture(BuildContext context, AsyncSnapshot snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.none:
        print('还没有开始网络请求');
        return Text('还没有开始网络请求');
      case ConnectionState.active:
        print('active');
        return Text('ConnectionState.active');
      case ConnectionState.waiting:
        print('waiting');
        return Center(
          child: CircularProgressIndicator(),
        );
      case ConnectionState.done:
        print('done');
        if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        }

        String user_name = snapshot.data;
        if (user_name != null && user_name.length > 0) {
          return baseTab();
        }

        return LoginHomePage(
          title: '优教信使',
        );
      default:
        return null;
    }
  }
}

class LoginHomePage extends StatelessWidget {
  LoginHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: LoginRootPage(title: title),
    );
  }
}

//void main() => runApp(_widgetForRoute(window.defaultRouteName));// 独立运行传入默认路由
//
//Widget _widgetForRoute(String route) {
//
//
//  switch (route) {
//    case "HomeRootPage":
//      return HomeRootPage(title:'优教信使');
//    default:
//      return MaterialApp(
//        home: Scaffold(
//          backgroundColor: const Color(0xFFD63031),//ARGB 红色
//          body: Center(
//            child: Text(
//              'Hello from Flutter', // 显示的文字
//              textDirection: TextDirection.ltr,
//              style: TextStyle(
//                fontSize: 20.0,
//                color: Colors.blue,
//              ),
//            ),
//          ),
//        ),
//      );
//  }
//}
